﻿using Auth.Application.IServices.System;
using Auth.Dtos.System;
using Auth.Entities.System;
using Auth.IRepository;
using Auth.Models;
using Auth.Models.System;
using AutoMapper;
using Dapper.Contrib.Plus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Auth.Application.Services.System
{
    /// <summary>
    /// 服务实现
    /// </summary>
    public class SettingService : ISettingService
    {
        /// <summary>
        /// 工作单元
        /// </summary>
        private readonly IUnitOfWork UnitOfWork;
        
        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper _mapper;

        public SettingService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            UnitOfWork = unitOfWork;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<Guid?> InsertAsync(SettingDto dto)
        {
            Setting entity = _mapper.Map<Setting>(dto);

            // 判断Value是否已存在
            if (await UnitOfWork.Context.HasValueAsync<Setting>(nameof(entity.Value), entity.Value))
            {
                return null;
            }
            else
            {
                await UnitOfWork.Context.InsertAsync(entity);
                return entity.Id;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(SettingDto dto)
        {
            Setting entity = _mapper.Map<Setting>(dto);
            bool result = await UnitOfWork.Context.DeleteAsync(entity);

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(SettingDto dto)
        {
            Setting entity = _mapper.Map<Setting>(dto);
            bool result = await UnitOfWork.Context.UpdateAsync(entity);

            return result;
        }

        /// <summary>
        /// 查询单条记录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<SettingDto> GetAsync(Guid id)
        {
            Setting entity = await UnitOfWork.Context.GetAsync<Setting>(id);
            SettingDto dto = _mapper.Map<SettingDto>(entity);

            return dto;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task<PagedList<SettingDto>> GetPagedListAsync(SettingModel model)
        {
            IEnumerable<Setting> entities = await UnitOfWork.Context.GetPagedListAsync<Setting, SettingModel>(model.Pagination.Page, model.Pagination.ItemsPerPage, model);
            long count = await UnitOfWork.Context.CountAsync<Setting, SettingModel>(model);

            // 如果有记录
            if (count > 0)
            {
                // 查询并关联上级节点的文本值
                IList<Condition> conditions = new List<Condition>
                {
                    new Condition {
                        Name = nameof(model.Value),
                        Value = entities.Where(s => !string.IsNullOrWhiteSpace(s.ParentValue)).Select(s => s.ParentValue).ToList(),
                        Type = ConditionalType.In
                    }
                };

                IEnumerable<Setting> plist = await UnitOfWork.Context.GetListAsync<Setting>(conditions);

                if (plist.Any())
                {
                    foreach (var item in plist)
                    {
                        entities.Where(s => s.ParentValue == item.Value).FirstOrDefault().Superior.Text = item.Text;
                        entities.Where(s => s.ParentValue == item.Value).FirstOrDefault().Superior.Value = item.Value;
                    }
                }
            }

            IEnumerable<SettingDto> list = _mapper.Map<IEnumerable<SettingDto>>(entities);
            return new PagedList<SettingDto> { List = list, Count = count };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<List<SettingDto>> GetListAsync(string key)
        {
            List<Condition> conditions = new List<Condition>
            {
                new Condition
                {
                    Name = "Value",
                    Value = key,
                    Type = ConditionalType.Equal
                }
            };

            var list = await UnitOfWork.Context.GetListAsync<Setting>(conditions);
            return _mapper.Map<List<SettingDto>>(list);
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<IEnumerable<SettingDto>> CountAsync(SettingModel model)
        {
            IEnumerable<Setting> entities = await UnitOfWork.Context.GetListAsync<Setting, SettingModel>(model);
            IEnumerable<SettingDto> list = _mapper.Map<IEnumerable<SettingDto>>(entities);

            return list;
        }
    }
}
